package word;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

/**
 * Java读取word中表格 https://www.cnblogs.com/GoForMyDream/p/8559072.html
 * Java处理Word文档 https://blog.csdn.net/weixin_43730516/article/details/117963993
 * 公司要把word表格的部分行列存到数据库中
 */
public class ReadWordUtil {

    public static void main(String[] args) throws Exception {
        // testWord("C:\\Users\\74102\\Desktop\\材料附件1-6.docx");
        readTable("C:\\Users\\74102\\Desktop\\材料附件1-6.docx");
    }

    public static void testWord(String filePath) {
        try {
            FileInputStream in = new FileInputStream(filePath);//载入文档
            if (filePath.toLowerCase().endsWith("docx")) {
                //如果是office2007 docx格式
                //word 2007 图片不会被读取， 表格中的数据会被放在字符串的最后
                XWPFDocument xwpf = new XWPFDocument(in); //得到word文档的信息
                // List<XWPFParagraph> listParagraphs = xwpf.getParagraphs(); //得到段落信息
                Iterator<XWPFTable> it = xwpf.getTablesIterator(); //得到word中的表格

                while (it.hasNext()) {

                    XWPFTable table = it.next();
                    List<XWPFTableRow> rows = table.getRows();
                    //读取每一行数据
                    for (int i = 0; i < rows.size(); i++) {
                        XWPFTableRow row = rows.get(i);
                        //读取每一列数据
                        List<XWPFTableCell> cells = row.getTableCells();
                        for (int j = 0; j < cells.size(); j++) {
                            XWPFTableCell cell = cells.get(j);
                            //输出当前的单元格的数据
                            System.out.println(cell.getText());
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * java读写word表格、图片
     * https://blog.csdn.net/qq_38759137/article/details/130083388
     *
     * @throws Exception
     */
    private static void readTable(String filePath) throws Exception {

        try (InputStream inputStream = new FileInputStream(filePath);
             XWPFDocument document = new XWPFDocument(inputStream);
        ) {
            List<XWPFParagraph> paragraphs = document.getParagraphs();
            List<IBodyElement> bodyElements = document.getBodyElements();
            for (IBodyElement bodyElement : bodyElements) {
                BodyElementType elementType = bodyElement.getElementType();
                if (elementType == BodyElementType.TABLE) {
                    //表格
                    System.out.println("table" + bodyElement.getPart());
                    XWPFTable table = (XWPFTable) bodyElement;
                    List<XWPFTableRow> rows = table.getRows();
                    //读取每一行数据
                    for (int i = 0; i < rows.size(); i++) {
                        XWPFTableRow row = rows.get(i);
                        //读取每一列数据
                        List<XWPFTableCell> cells = row.getTableCells();
                        for (int j = 0; j < cells.size(); j++) {
                            XWPFTableCell cell = cells.get(j);
                            System.out.println(cell.getText());
                            List<XWPFParagraph> cellParagraphs = cell.getParagraphs();
                            System.out.println(cellParagraphs.size());
                        }
                    }
                } else {
                    //段落
                    XWPFParagraph para = (XWPFParagraph) bodyElement;
                    // 本段缩进 indention
                    System.out.println("It is a new paragraph...The indention is " + para.getFirstLineIndent() + ", 对齐方式是：" + para.getAlignment());

                    List<XWPFRun> runs = para.getRuns();
                    System.out.println("run");
                    if (runs.size() <= 0) {
                        System.out.println("empty line");
                    }
                    // 遍历每个run
                    for (XWPFRun run : runs) {
                        //如果片段没有文字，可能该片段是图片
                        if (StringUtils.isEmpty(run.text())) {
                            //该片段是图片时
                            if (run.getEmbeddedPictures().size() > 0) {
                                System.out.println("image***" + run.getEmbeddedPictures());
                            } else {
                                System.out.println("objects:" + run.getCTR().getObjectList());
                                //公式
                                if (run.getCTR().xmlText().indexOf("instrText") > 0) {
                                    System.out.println("there is an equation field");
                                }
                            }
                        } else {
                            System.out.println("===" + run.getCharacterSpacing() + run.text());
                        }
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


    }

}
